﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace TrafficAccessibilityIndex
{

    public enum AnalysisType
    {
        /// <summary>
        /// 距离最短
        /// </summary>
        ByLength,
        /// <summary>
        /// 时间最短
        /// </summary>
        ByTime
    }

    public partial class FormParameters : Form
    {
        private string _speedFiledName = "";

        public string SpeedFiledName
        {
            get { return _speedFiledName; }
        }


        private AxMapControl axMapControl;
        private ILayer _roadLayer = null;

        public ILayer RoadLayer
        {
            get { return _roadLayer; }
        }

        private ILayer _poiLayer1 = null;

        public ILayer POILayer1
        {
            get { return _poiLayer1; }
        }

        private ILayer _poiLayer2 = null;

        public ILayer POILayer2
        {
            get { return _poiLayer2; }
        }


        private ILayer _poiLayer3 = null;

        public ILayer POILayer3
        {
            get { return _poiLayer3; }
        }

        private ILayer _pointLayer = null;

        public ILayer POINTLayer
        {
            get { return _pointLayer; }
        }

        private ILayer _rasterLayer = null;

        public ILayer RasterLayer
        {
            get { return _rasterLayer; }
        }

        private AnalysisType _analysisType = AnalysisType.ByLength;

        public AnalysisType AnalysisType
        {
            get { return _analysisType; }
        }


        public FormParameters()
        {
            InitializeComponent();
        }

        public FormParameters(AxMapControl axMapControl)
            : this()
        {
            this.axMapControl = axMapControl;
        }

        private void FormParameters_Load(object sender, EventArgs e)
        {
            this.comboBoxAnalysisType.SelectedIndex = 0;
            int layerCount = this.axMapControl.LayerCount;
            for (int i = 0;  i < layerCount; ++i)
            {
                string name = this.axMapControl.get_Layer(i).Name;
                //this.comboBoxLayerRoad.Items.Add(name);
                this.comboBoxLayerRaster.Items.Add(name);
                this.comboBoxPOI.Items.Add(name);
                this.comboBox2.Items.Add(name);
                this.comboBox3.Items.Add(name);
                this.comboBox1.Items.Add(name);
            }
        }

        //private void comboBoxLayerRoad_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    string layerName = this.comboBoxLayerRoad.SelectedItem.ToString();
        //    this._roadLayer = GetLayerByName(layerName);

        //    IFeatureLayer feaLayer = this.RoadLayer as IFeatureLayer;
        //    IFields fields = feaLayer.FeatureClass.Fields;
        //    int count = fields.FieldCount;
        //    for (int i = 0; i < count; ++i)
        //    {
        //        IField field = fields.get_Field(i);
        //        this.comboBoxSpeedFiled.Items.Add(field.Name);
        //    }

        //}

        private ILayer GetLayerByName(string layerName)
        {
            int layerCount = this.axMapControl.LayerCount;
            for (int i = 0; i < layerCount; ++i)
            {
                ILayer result = this.axMapControl.get_Layer(i);
                string name = result.Name;
                if(name == layerName)
                {
                    return result;
                }
            }
            return null;
        }

        private void comboBoxLayerRaster_SelectedIndexChanged(object sender, EventArgs e)
        {
            string layerName = this.comboBoxLayerRaster.SelectedItem.ToString();
            this._rasterLayer = GetLayerByName(layerName);
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if ( this.comboBoxLayerRaster.SelectedIndex == -1)
            {
                MessageBox.Show("请选择栅格图层");
                return;
            }

            if (this.comboBoxAnalysisType.SelectedIndex == 0)
            {
                _analysisType = TrafficAccessibilityIndex.AnalysisType.ByLength;
            }
            else 
            {
                _analysisType = TrafficAccessibilityIndex.AnalysisType.ByTime;
            }

            //if (this.comboBoxSpeedFiled.SelectedIndex != -1)
            //{
            //    _speedFiledName = this.comboBoxSpeedFiled.SelectedItem.ToString();
            //}

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Dispose();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void comboBoxPOI_SelectedIndexChanged(object sender, EventArgs e)
        {
            string layerName = this.comboBoxPOI.SelectedItem.ToString();
            this._poiLayer1 = GetLayerByName(layerName);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string layerName = this.comboBox1.SelectedItem.ToString();
            this._pointLayer = GetLayerByName(layerName);
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string layerName = this.comboBox2.SelectedItem.ToString();
            this._poiLayer2 = GetLayerByName(layerName);
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            string layerName = this.comboBox3.SelectedItem.ToString();
            this._poiLayer3 = GetLayerByName(layerName);
        }

    }
}
